﻿(function () {
    var myApp = angular.module('MyControllers');

    // 计划列表所需控制器
    myApp.controller('signInPlanController', ['$scope', '$http', '$modal', 'signService', 'utils', function ($scope, $http, $modal, service, utils) {
        $scope.getList = function () {
            service.getPlanList($scope.planType).success(function (data) {
                $scope.list = data;
            });
        };

        $scope.official = function () {
            $scope.planType = 0;
            $scope.getList();
        };

        $scope.imatate = function () {
            $scope.planType = 1;
            $scope.getList();
        };

        $scope.detail = function (model) {
            $scope.loadingState = true;

            var parameter = {
                planId: model.examPlanInfoID,
                planName: model.examPlanName,
                examMode: model.examMode
            };

            utils.encrypt(JSON.stringify(parameter)).success(function (data) {
                if (data.resultType == 'Success') {
                    window.location.href = '#/signIn/list?parameter=' + data.appendData;
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };

        $scope.official();
    }]);

    // 签到所需控制器
    myApp.controller('signInController', ['$scope', '$http', '$location', '$modal', 'signService', 'utils', function ($scope, $http, $location, $modal, service, utils) {
        var planId;

        $scope.init = function () {
            $scope.loadingState = true;
            utils.decrypt($location.$$search.parameter).success(function (data) {
                if (data.resultType == 'Success') {
                    var result = JSON.parse(data.appendData);

                    $scope.examMode = result.examMode;

                    planId = result.planId;
                    $scope.planName = result.planName;
                    $scope.getSceneList();
                } else {
                    utils.notify(data.message, 'warning');
                }

                $scope.loadingState = false;
            });
        };

        $scope.getSceneList = function (roomId, subjectId) {
            service.getSceneList(planId, roomId, subjectId).success(function (data) {
                angular.forEach(data, function (item) {
                    item.examSceneName += ' (' + utils.formatDate(item.planBeginTime) + ')';
                });

                $scope.sceneList = data;
                $scope.curScene = $scope.sceneList[0];

                $scope.getRoomList($scope.curScene.examSceneID);
            });
        };

        $scope.getRoomList = function (sceneId, subjectId) {
            service.getRoomList(planId, sceneId, subjectId).success(function (data) {
                data.unshift({ examRoomID: null, examRoomName: '全部考场' });

                $scope.roomList = data;
                $scope.curRoom = $scope.roomList[0];

                $scope.getStatusList();
            });
        };

        $scope.getStatusList = function () {
            var data = [{ statusId: null, statusName: '全部状态' }, { statusId: 0, statusName: '未签到' }, { statusId: 1, statusName: '系统签到' }, { statusId: 2, statusName: '人工签到' }];

            $scope.statusList = data;
            $scope.curStatus = $scope.statusList[0];

            $scope.query();
        };

        $scope.query = function () {
            var curScene = $scope.curScene;
            var curRoom = $scope.curRoom;
            var curStatus = $scope.curStatus;
            var parameter = $scope.curParams;

            $scope.loadGrid(curScene.examSceneID, curRoom.examRoomID, curStatus.statusId, parameter);
        };

        // 这里借助 ng-change 更新options的值，其他页面相同的情况为什么可以？
        $scope.changeScene = function (model) { $scope.curScene = model; }
        $scope.changeRoom = function (model) { $scope.curRoom = model; }

        $scope.loadGrid = function (sceneId, roomId, statusId, parameter) {
            $scope.loadingState = true;

            if ($scope.examMode == 3) {
                sceneId = undefined;
                roomId = undefined;
            }

            service.getList(planId, sceneId, roomId, statusId, parameter).success(function (data) {
                $scope.loadingState = false;

                if (data.resultType == 'Success') {
                    $scope.list = data.appendData;

                    angular.forEach($scope.list, function (data, index, array) {
                        // 统考签到情况
                        if ($scope.examMode != 3 && data.IDCardResult > 0) {
                            data.todayIDCard = true;
                        }
                            // 随考签到情况
                        else if ($scope.examMode == 3 && data.IDCardResult > 0 && utils.isToday(data.IDCardTime)) {
                            data.todayIDCard = true;
                        } else {
                            data.todayIDCard = false;
                        }
                    });
                } else {
                    utils.notify(data.message, 'warning');
                }
            });
        };

        $scope.automatic = function () {
            $modal.open({
                templateUrl: '/App/views/SignIn/Automatic.html',
                controller: 'automatic',
                backdrop: 'static',
                keyboard: false,
                resolve: {
                    params: function () {
                        return { planId: planId, examMode: $scope.examMode }
                    }
                }
            });
        }

        $scope.help = function () {
            $modal.open({
                templateUrl: '/App/views/SignIn/Help.html',
                controller: 'help',
                resolve: {
                    params: function () {
                        return 0;
                    }
                }
            });
        }

        $scope.manual = function (model) {
            if (model.todayIDCard) {
                utils.notify('当前学生已签到！', 'warning');
            } else {
                $modal.open({
                    templateUrl: '/App/views/SignIn/Manual.html',
                    controller: 'manual',
                    backdrop: 'static',
                    keyboard: false,
                    resolve: {
                        params: function () {
                            return { model: model, examMode: $scope.examMode }
                        }
                    }
                });
            }
        }

        $scope.init();

        $scope.$root.$on('loadGrid', function () {
            $scope.query();
        });
    }]);

    // 人工签到需要的控制器及方法列表
    myApp.controller('manual', ['$scope', '$routeParams', '$modalInstance', 'signService', '$modal', 'utils', 'params', '$filter', function ($scope, $routeParams, $modalInstance, service, $modal, utils, params, $filter) {
        var examMode = params.examMode;

        $scope.monitor = params.model;
        $scope.examinee = params.model;

        var methods = {
            save: function () {
                if (!$scope.monitor.IDCardReason) {
                    utils.notify('人工确认的理由不能为空！', 'warning');
                } else {
                    $scope.loadingState = true;

                    service.manualSignIn(examMode, $scope.monitor).success(function (data) {
                        $scope.loadingState = false;

                        if (data.resultType == 'Success') {
                            $scope.close();
                            $scope.$emit('loadGrid');
                            utils.notify(data.message, 'success');
                        } else {
                            utils.notify(data.message, 'warning');
                        }
                    });
                }
            },
            close: function () {
                $modalInstance.close(true);
            }
        };

        angular.extend($scope, methods);
    }]);

    // 系统签到需要的控制器及方法列表
    myApp.controller('automatic', ['$scope', '$interval', '$routeParams', '$modalInstance', 'signService', '$modal', 'utils', 'params', '$filter', function ($scope, $interval, $routeParams, $modalInstance, service, $modal, utils, params, $filter) {
        var planId = params.planId, examMode = params.examMode;

        var methods = {
            initTimer: function () {
                var br = navigator.userAgent.toLowerCase();
                var isIe = br.indexOf('trident') > -1 || br.indexOf('msie') > -1;

                if (isIe) {
                    var timer3 = $interval(function () {
                        if ($scope.IsAuthenticate()) {
                            var data = $scope.readIdCard();
                            $scope.save(data);
                        }
                    }, 100);
                    utils.setData({ timer3: timer3 });
                } else {
                    utils.notify('请使用IE浏览器进行签到操作！\n请注意Edge浏览器并不是IE浏览器！', 'danger');
                }
            },
            IsAuthenticate: function () {
                try {
                    // 确认当前机具上是否有卡
                    var obj = document.getElementById('obj');
                    return obj.IsAuthenticate();
                }
                catch (e) {
                    utils.notify(e.messgae, 'danger');
                    return false;
                }
            },
            readIdCard: function () {
                try {
                    var obj = document.getElementById('obj');
                    var data = JSON.parse(obj.ReadBaseInfos());
                    return data;
                }
                catch (e) {
                    utils.notify(e.messgae, 'danger');
                    return null;
                }
            },
            save: function (info) {
                $scope.loadingState = true;
                // 测试
                //info.Name = '周伟';
                //info.Code = '430721199010237054';

                service.automaticSignIn(planId, examMode, info.Name, info.Code).success(function (data) {
                    $scope.loadingState = false;
                    if (data.resultType == 'Success') {
                        $scope.stuInfo = info;
                        //$scope.stuInfo.Photo = 'D:\\photo.jpg';
                        $scope.arrangeList = data.appendData;
                        utils.notify(data.message, 'success');
                        $scope.$emit('loadGrid');
                    } else {
                        $scope.stuInfo = undefined;
                        $scope.arrangeList = undefined;

                        utils.notify(data.message, 'warning');
                    }
                });
            },
            close: function () {
                $modalInstance.close(true);
            }
        };

        angular.extend($scope, methods);

        $scope.initTimer();
    }]);

    // 使用帮助需要的控制器及方法列表
    myApp.controller('help', ['$scope', '$modalInstance', function ($scope, $modalInstance) {
        $scope.url = location.origin;

        var methods = {
            close: function () {
                $modalInstance.close(true);
            }
        };

        angular.extend($scope, methods);
    }]);
})();